<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户列表</title>
    <link rel="stylesheet" type="text/css" th:href="@{/js/layui/css/layui.css}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/fyn/style.css/}" media="all">
    <link rel="stylesheet" type="text/css" th:href="@{/css/fyn/my.css/}" media="all">

</head>
<body class="body">

<script type="text/html" id="topOperate">
    <div class="my-btn-box layui-form">
      <span class="fl">
        <button class="layui-btn layui-btn-sm" lay-event="toSave"><i
                class="layui-icon"></i>添加用户</button>
        <button id="codeId" style="display: none" class="layui-btn layui-btn-danger layui-btn-sm" lay-event="resetCode"><i
                class="layui-icon layui-icon-refresh-3"></i>重置谷歌验证码</button>
      </span>

    </div>
</script>

<div class="my-btn-box layui-form">
    <span class="fr">
        <div class="layui-input-inline">
            <input type="text" autocomplete="off" name="name" placeholder="请输入用户名称" class="layui-input height33">
        </div>
        <button class="layui-btn layui-btn-sm" id="search"><i class="layui-icon">&#xe615;</i></button>
    </span>
</div>

<!-- 表格 -->
<div id="dateTable" lay-filter="event"></div>

<div class="height50"></div>
<script th:src="@{/js/layui/layui.js}"></script>
<script th:src="@{/js/fyn/index.js}"></script>
<script th:src="@{/js/fyn/my.js}"></script>
<script type="text/javascript" th:src="@{/js/vip/vip_comm.js}"></script>
<script type="text/javascript" th:src="@{/js/vip/vip_table.js}"></script>
<script type="text/javascript" th:inline="javascript">

    // layui方法
    layui.use(['table', 'form', 'laypage', 'layer', 'vip_table'], function () {

        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , vipTable = layui.vip_table
            , $ = layui.jquery;

        var user = [[${user}]];

        // 表格渲染
        var tableIns = table.render({
            elem: '#dateTable'                  //指定原始表格积分素选择器（推荐id选择器）
            , height: "full"    //容器高度
            // , skin: 'line'
            , even: 'true'
            , cols: [[                  //标题栏
                {
                    field: 'user.id', title: 'ID', minWidth: '100', sort: true, align: 'center', templet: function (d) {
                        return d.user.id;
                    }
                }, {
                    field: 'user.username', title: '登陆名', align: 'center', minWidth: '100', templet: function (d) {
                        return d.user.username;
                    }
                }
                , {
                    field: 'user.name', title: '用户名称', align: 'center', minWidth: '100', templet: function (d) {
                        return d.user.name;
                    }
                }
                , {
                    field: 'role.name', title: '角色名称', minWidth: '100', align: 'center', templet: function (d) {
                        return d.role.name;
                    }
                }
                , {
                    field: 'status', title: '状态', minWidth: '70', align: 'center', event: 'updateS', templet: function (d) {
                        return "<input lay-filter='status' disabled  type='checkbox' name='status'" + (d.user.status == 0 ? "checked" : "") + " lay-skin=\"switch\" lay-text=\"开|关\" class=\"status\">";
                        //return d.user.status == 0 ? '<i class="layui-icon" style="font-size: 20px; color: green;">&#xe605;</i>' : "<i class=\"layui-icon\" style=\"font-size: 20px; color: green;\">&#x1006;</i>";
                    }
                }
                , {
                    field: 'user.ctime',
                    align: 'center',
                    title: '创建时间',
                    sort: true,
                    minWidth: '150',
                    templet: function (d) {
                        return new Date(d.user.ctime).Format("yyyy-MM-dd hh:mm:ss");
                    }
                }
                , {
                    field: 'user.memo', title: '备注', minWidth: '100', align: 'center', templet: function (d) {
                        return d.user.memo == null ? '' : d.user.memo;
                    }
                }
                , {title: '操作', align: 'center',minWidth: '350', toolbar: '#barOption'} //这里的toolbar值是模板积分素的选择器
            ]]
            , id: 'dataCheck'
            , url: [[${basePath}]] + '/user/list'
            , method: 'get'
            , page: true
            , limits: [10, 30, 60, 90, 150, 300]
            , toolbar: '#topOperate'
            , limit: 10 //默认采用0
            , loading: true
            // ,where:{name:$("input[name='name']").val()}
            , done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                $("td[data-field='content']").each(function () {
                    $(this).find(".layui-table-cell").removeClass('layui-table-cell');
                });

                // admin用户展示重置谷歌验证码
                if (null != user) {
                    var username= user.username;
                    if (username == "admin") {
                        $("#codeId").show();
                    }
                    else{
                        $("#codeId").hide();
                    }
                }

            }
        });


        //头工具栏事件
        table.on('toolbar(event)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;

            switch (obj.event) {
                case 'toSave':
                    layer.open({
                        type: 2,
                        title: '用户增加',
                        shadeClose: true,
                        shade: 0.2,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['650px', '850px'],
                        content: [[${basePath}]] + '/user/save/index'
                    });
                    break;
                case 'resetCode':
                    layer.confirm("确定重置所有用户的谷歌验证码?", {
                        icon: 3,
                        title: '提示'
                    }, function (index) {
                        $.ajax({
                            type: "post",
                            url: [[${basePath}]] + "/verify/resetAllGoogleQRCode",
                            data: {},
                            success: function (res) {
                                if (res.success) {
                                    layer.msg(res.msg, {icon: 1});
                                    layer.close(index);
                                    return;
                                }
                                layer.msg("操作失败！", {icon: 2});
                            }
                        });
                    });
                    break;
            }
        });

        table.on('tool(event)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                layer.open({
                    type: 2,
                    title: '用户编辑',
                    shadeClose: true,
                    shade: 0.2,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['650px', '850px'],
                    content: [[${basePath}]] + '/user/save/index?id=' + data.user.id
                });

            }
            else if (obj.event === 'updateS') {

                layer.confirm("确定" + (data.user.status == 0 ? "暂停(账号不能再登陆)" : "开启") + "账号状态?", {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        type: "post",
                        url: [[${basePath}]] + "/user/status/update",
                        data: {id: data.user.id},
                        success: function (res) {
                            if (res.success) {
                                layer.msg("操作完成", {icon: 1});
                                setTimeout(function () {

                                }, 1500);
                                layer.close(index);

                                tableIns.reload();
                            }
                        }
                    });


                }, function (index) {
                    console.log(data.user.status);
                    obj.update({
                        status: "1111"
                    });

                });
            }
            else if (obj.event === 'reset') {

                layer.confirm('确定重置此账号密码为<span style="color: red;font-weight: bold;">000000</span>?', {icon: 3, title: '提示'}, function (index) {

                    $.ajax({
                        type: "post",
                        url: [[${basePath}]] + "/user/password/reset",
                        data: {id: data.user.id},
                        success: function (data) {
                            if (data.success) {
                                layer.msg("重置密码成功！", {icon: 1});

                            }
                        }
                    });


                    layer.close(index);
                });

            }
            else if (obj.event === 'del') {
                layer.confirm('确定要删除此账号?', {icon: 3, title: '提示'}, function (index) {
                    $.ajax({
                        type: "post",
                        url: [[${basePath}]] + "/user/delete",
                        data: {id: data.user.id},
                        success: function (data) {
                            if (data.success) {
                                tableIns.reload(); // 重构表格
                                layer.msg("删除成功！", {icon: 1});
                                return
                            }
                        }
                    });
                    layer.close(index);
                });

            }
            else if (obj.event === 'googleCode') {
                layer.open({
                    type: 2,
                    title: '绑定列表',
                    shadeClose: true,
                    area: ['800px', '500px'],
                    content: [[${basePath}]] + '/verify/googleCodeList'
                });

            }
        });


        $("#search").click(function () {
            tableIns.reload({
                where: {name: $("input[name='name']").val()},
                page: {curr: 1}
            });
        })


    });
</script>
<!-- 表格操作按钮集 -->
<script type="text/html" id="barOption">
    <!--<i class="layui-icon layui-icon-radio" lay-event="reset" title="重置密码"></i>-->
    <!--<i class="layui-icon layui-icon-edit" lay-event="edit"></i>-->
    <a class="layui-btn layui-bg-green layui-btn-xs" lay-event="googleCode"><i class="layui-icon layui-icon-vercode"></i>google验证码</a>
    <a class="layui-btn layui-bg-green layui-btn-xs" lay-event="reset"><i class="layui-icon layui-icon-refresh"></i>重置密码</a>
    <a class="layui-btn layui-bg-orange layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
    <a class="layui-btn layui-bg-red layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
</body>
</html>